<template>
  <my-dialog :subLoading="subLoading" @on-submit="submitHandle" :readOnly="readOnly">
    <my-header slot="header" project-sub-text="延误影响分析"  :project-sub-title="`业主代表检查日期: ${ownerExamine.created_at||''}`" :project-name="projectDetail.project_name || ''"></my-header>
    <div slot="body">
      <widgets title="施工单位" :value="projectDetail.construction_organeation|| ''" icon="organization"></widgets>
      <widgets title="项目负责人" :value="projectDetail.project_manager || ''" icon="responsibility"></widgets>
      <widgets title="监理单位" :value="projectDetail.jianli_company || ''" icon="supervisor"></widgets>
      <widgets title="监理项目负责人" :value="projectDetail.jianli_name || ''" icon="responsibility"></widgets>
      <widgets title="标段名称" :value="projectDetail.sction_name|| ''" icon="section"></widgets>
      <my-textarea v-model="ownerExamine.work_performance" :disabled="true" label-text="工程量完成情况"
                   placeholder="请填写工程量完成情况" error-message="请填写工程量完成情况"></my-textarea>

      <my-textarea v-model="ownerExamine.people_condition" :disabled="true" label-text="人员及机械投入情况"
                   placeholder="请填写人员及机械投入情况" error-message="请填写人员及机械投入情况"></my-textarea>

      <my-textarea v-model="ownerExamine.day_condition" :disabled="true" label-text="日，周，月进度完成情况"
                   placeholder="请填写日，周，月进度完成情况" error-message="请填写日，周，月进度完成情况"></my-textarea>

      <my-textarea v-model="ownerExamine.people_post" :disabled="true" label-text="施工，监理管理人员在岗情况"
                   placeholder="请填写施工，监理管理人员在岗情况" error-message="请填写施工，监理管理人员在岗情况"></my-textarea>

      <my-textarea v-model="ownerExamine.progress_condition" :disabled="true" label-text="进度计划与实际进度的对比情况"
                   placeholder="请填写进度计划与实际进度的对比情况" error-message="请填写进度计划与实际进度的对比情况" ></my-textarea>

      <my-textarea v-model="ownerExamine.analyze" :disabled="true" label-text="进度延误分析（非必填）"
                   placeholder="请填写进度延误分析" :required="false"></my-textarea>

      <check-box v-model="ownerExamine.approveBoole" :disabled="true" lbl-text="是否满足延误进度处罚条件"></check-box>
      <down  :files="projectFiles"></down>
      <widgets icon="times" class="widget-header" title="施工单位延误影响分析" :value="completeTimeDiff || ''"></widgets>
      <my-textarea v-model="crtPlan" :disabled="readOnly" label-text="进度分析" placeholder="请输入进度分析" error-message="请输入进度分析"></my-textarea>
      <my-textarea v-model="comment" :disabled="readOnly" label-text="进度纠偏措施" placeholder="请输入进度纠偏措施" error-message="请输入进度纠偏措施"></my-textarea>
      <check-box v-model="available" :disabled="readOnly" lbl-text="是否调整进度计划"></check-box>
    </div>
  </my-dialog>
</template>

<script>
  import Dialog from "@/components/Dialog";
  import Header from "@/components/Dialog/Header";
  import Widgets from "@/components/Dialog/Widgets";
  import TextArea from "@/components/Dialog/TextArea";
  import Down from "@/components/Dialog/Down";
  import CheckBox from "@/components/Dialog/Check";
  import  { mapGetters } from "vuex";
  import _ from 'lodash'
  import taskApi from '@/api/task'
  import projectApi from '@/api/project'
  import moment from 'moment'
  moment.locale('zh-CN')

  export default {
    computed: {
      ...mapGetters([
        'userData'
      ]),
      projectFiles () {
        return _.filter(this.projectDetail.file, file => {
          return file.type === 4
        })
      },
      completeTimeDiff () {
        if (this.taskDetail.do_task_at && this.taskDetail.status === 0) {
          const complateTime = moment(this.taskDetail.do_task_at).add(2, 'days')
          const diffHours = complateTime.diff(moment(), 'hours')
          const diffMin = complateTime.subtract(diffHours, 'hours').diff(moment(), 'minutes')
          if (diffMin < 0) {
            return `已超过处理时间${-diffHours}小时${-diffMin}分钟`
          }
          return `距离最后的处理时间剩余：${diffHours}小时${diffMin}分钟`
        }
        if (this.taskDetail.status === 1) {
          return `已处理，上报日期：${this.masterCreated}`
        }
        return ''
      }
    },
    data () {
      return {
        subLoading: false,
        readOnly: false,
        crtPlan: '',
        comment: '',
        available: true,
        projectDetail: {
          file: []
        },
        taskDetail: {},
        ownerExamine: {
          is_approve: 0,
          work_performance: '',
          people_condition: '',
          day_condition: '',
          people_post: '',
          progress_condition: '',
          analyze: '',
          approveBoole: true
        },
        masterCreated: ''
      }
    },
    components: {
      'MyDialog': Dialog,
      'MyHeader': Header,
      Widgets,
      Down,
      'MyTextarea': TextArea,
      CheckBox
    },
    created () {
      this.getProjectInfo()
      this.getTaskInfo(() => {
        this.getCheckPlanDetail()
      })
    },
    methods: {
      getProjectInfo () {
        const projectId = this.$route.params.projectId
        this.$store.dispatch('getSingeProject', {token: this.userData.token, pId: projectId}).then(res => {
          this.projectDetail = res.data
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      },
      getTaskInfo (cb) {
        const taskId = this.$route.params.taskId
        taskApi.getTaskInfo(this.userData.token, taskId).then(res => {
          const result = res.data
          this.taskDetail = result.data
          if (cb) {
            cb()
          }
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      },
      getCheckPlanDetail () {
        const checkPlanId = this.taskDetail.check_plan_id
        projectApi.getCheckPlanDetail(this.userData.token, checkPlanId).then(res => {
          const result = res.data.data
          this.ownerExamine = result.ownerExamineIsfinish.data
          this.ownerExamine.approveBoole = (this.ownerExamine.is_approve === 0)
          if (result.constructorAnalyzeCause.data.id) {
            this.readOnly = true
            this.comment = result.constructorAnalyzeCause.data.improvement_measures
            this.crtPlan = result.constructorAnalyzeCause.data.analyze
            this.masterCreated = result.constructorAnalyzeCause.data.created_at
            this.available = result.constructorAnalyzeCause.data.is_modification === 0
          }
        })
      },
      submitHandle () {
        if (!this.crtPlan) {
          this.$message.error('请输入进度分析')
          return
        }
        if (!this.comment) {
          this.$message.error('请输入进度纠偏措施')
          return
        }
        this.subLoading = true
        const projectId = this.$route.params.projectId
        const taskId = this.$route.params.taskId
        const checkPlanId = this.taskDetail.check_plan_id
        taskApi.constructionAnalyze(this.userData.token, {
          task_id: taskId,
          project_id: projectId,
          check_plan_id: checkPlanId,
          is_modification: this.available ? 0 : 1,
          users_id: this.projectDetail.users_id,
          improvement_measures: this.comment,
          analyze: this.crtPlan
        }).then(res => {
          this.$message.success('提交成功')
          this.subLoading = false
          this.$router.back()
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      }
    }
  }
</script>
